package com.wc.alorithm_luogu._DFS.快速求和;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/10/24 15:05
 * @description https://www.luogu.com.cn/problem/P1874
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 45;
    static char[] cs = new char[N];
    static int n, m, res;

    public static void main(String[] args) {
        cs = sc.next().toCharArray();
        n = cs.length;
        m = sc.nextInt();
        res = n;
        dfs(0, 0, 0);
        out.println(res == n ? -1 : res);
        out.flush();
    }

    static void dfs(int start, int sum, int k) {
        if (sum > m || k - 1 >= res) return;
        if (start >= n) {
            if (sum == m) res = Math.min(res, k - 1);
            return;
        }
        int num = 0;
        while (cs[start] == '0' && start < n) start++;
        for (int i = start; i <= Math.min(n - 1, start + 5); i++) {
            num = num * 10 + cs[i] - '0';
            dfs(i + 1, sum + num, k + 1);
        }
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
